package Function;


import FCRendering.FCMapAnimationConstant;
import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRMaster;
import VWorldGameStudio.*;
import VWorldGameStudio.events.JumpEvent;
import VWorldGameStudio.events.StepOnEvent;
import com.google.gson.Gson;
import io.reactivex.schedulers.Schedulers;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import utils.ClickHelper;
import utils.FCMixUtil;
import utils.entity.EventCmdEntity;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.*;


//自动触发型事件的处理
public class FCAutoTriggerEventHelper {
    Gson gson = new Gson();

    private String lastEventName;
    private ClickHelper clickHelper = new ClickHelper(500);

    public void dealAutoTriggerFcEvents(FCEvent event) {
        if (!event.getType().equals("stepOn")) {
            return;
        }
        StepOnEvent stepOnEvent = (StepOnEvent) event;
        System.out.println("getContainCommand " + stepOnEvent.getContainCommand());
        lastEventName = event.getName();
        EventCmdEntity entity = gson.fromJson(stepOnEvent.getContainCommand(), EventCmdEntity.class);
        switch (entity.cmdType) {
            case EventCmdEntity.CmdTypeJuXingPaoBoom:
                FCMapAnimation animation = mapAnimationLibrary.getMapAnimationByName(FCMapAnimationConstant.JuXingPaoMapAni);
                if (animation != null) {
                    animation.resetAnimation();
                    String xyKey = (player1.getPlayer1x() - 1) + "*" + (player1.getPlayer1y() - 1);
                    FCMap map = mapLib.getMapOfName(player1.getMapname());
                    map.dynamicAddMapAnimation(FCMapAnimationConstant.JuXingPaoMapAni, xyKey);
                    animation.setVisiable(1);
                    Schedulers.io().scheduleDirect(juXingPaoRun, 900, TimeUnit.MILLISECONDS);
                }
                break;
            case EventCmdEntity.CmdTypeForSlide:
                fcv.setSlideDir(entity.param1);
                fcrMaster.slideMoveNDistanceWithDir(entity.param2, entity.param1);
                break;
            case EventCmdEntity.CmdTypeSaveChuansong:
                String mapName = player1.getMapname();
                if (entity.mapName != null) {
                    mapName = entity.mapName;
                }
                if (stepOnEvent.getContainOtherEventId() != null) {
                    // if (!gameSave.getChuanZhenMaps().containsKey(mapName)) {
                    gameSave.getChuanZhenMaps().put(mapName, stepOnEvent.getContainOtherEventId());
                    //   }
                }

                if (entity.containOtherEventId_2 != null) {
                    gameSave.getTankChuanZhenMaps().put(mapName, entity.containOtherEventId_2);
                }
                break;
            case EventCmdEntity.CmdTypeStartChuansong:
                if (!fcv.isInTeamLink()) {
                    if (fcv.getWillChuangSongMapName() != null) {
                        if (clickHelper.canClick()) {
                            clickHelper.setClickLock(true);
                            //等待一秒再传走哈哈哈
                            String jumpEventId = gameSave.getChuanZhenMaps().get(fcv.getWillChuangSongMapName());
                            System.out.println("jumpEventId " + jumpEventId);
                            FCEvent event1 = eventLib.getEventOfName(jumpEventId);
                            if (event1 == null && !event1.getType().equals("jump")) {
                                clickHelper.setClickLock(false);
                                return;
                            }
                            JumpEvent jumpEvent = (JumpEvent) event1;
                            fcv.setPlayingEvent(jumpEvent);
                            sound.setEffectSound(jumpEvent.getMusicnum0());
                            fcv.setJumpactionnum(0);
                            //TODO 通知队友也要转场了
                            if (fcv.isSever()) {
                                FCRSocketServer.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                            } else if (fcv.isClient()) {
                                FCRSocketClient.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                            }
                            fcv.setWillChuangSongMapName(null);

                            Schedulers.io().scheduleDirect(new Runnable() {
                                @Override
                                public void run() {
                                    FCMapAnimation chuanzhenAni = mapAnimationLibrary.getMapAnimationByName(FCMapAnimationConstant.ChuanZhenName);
                                    if (chuanzhenAni != null) {
                                        chuanzhenAni.setVisiable(0);
                                    }
                                    clickHelper.setClickLock(false);
                                }
                            }, 3, TimeUnit.SECONDS);
                        }
                    }
                }
                break;
        }
    }


    private Runnable juXingPaoRun = new Runnable() {
        @Override
        public void run() {
            ArrayList<FCPlayerInfo> needSyncRemoteBlood = new ArrayList<>();
            player1.setBlood(0);
            gameSave.useTankOfPlayerId(player1, player1.getPlayertank1(), player1.getPlayertankHostId());
            needSyncRemoteBlood.add(player1);
            for (FCRPlayerLink fcrPlayerLink : FCMutilPlayCache.getTeamMembers()) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(fcrPlayerLink.curPlayerHostUID, fcrPlayerLink.curPlayerName);
                if (p != null) {
                    p.setBlood(0);
                    needSyncRemoteBlood.add(p);
                    if (p.getPlayertank1() != null && !p.getPlayertank1().equals("null") && p.getAliasUID().equals(gameSave.HOST_UID)) {
                        gameSave.useTankOfPlayerId(p, p.getPlayertank1(), p.getPlayertankHostId());
                        //下车
                    }
                }
            }

            //TODO 同步给其他队友血量减少了,
            if (FCRMaster.fcv.isSever()) {
                FCRSocketServer.getIn().syncPlayerState(needSyncRemoteBlood);
            } else if (FCRMaster.fcv.isClient()) {
                FCRSocketClient.getIn().syncPlayerState(needSyncRemoteBlood);
            }


            if (!fcv.isInTeamLink()) {
                //是否队长死了，
                if (player1.isDeath()) {
                    fcv.setPlayingEvent(eventLib.getEventOfName("WelcomeToMingQi"));
                    fcv.setJumpactionnum(0);
                    JumpEvent jumpEvent = (JumpEvent) fcv.getPlayingEvent();
                    sound.setEffectSound(jumpEvent.getMusicnum0());
                    //TODO 通知队友也要转场了
                    if (FCRMaster.fcv.isSever()) {
                        FCRSocketServer.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                    } else if (FCRMaster.fcv.isClient()) {
                        FCRSocketClient.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                    }

                    //TODO 送你去见明奇
                    FCRMaster.fcrMaster.startToSeeMingQi();
                }
            }
        }
    };
}
